package activity.setting;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import com.hichip.callback.ICameraIOSessionCallback;
import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;
import com.hichip.control.HiCamera;

import activity.CustomView.CropImageView;
import base.HiActivity;
import bean.MyCamera;
import butterknife.BindView;
import common.HiDataValue;
import common.TitleView;

public class AdjustVMDActivity extends HiActivity implements ICameraIOSessionCallback, View.OnClickListener {
    @BindView(R.id.title) TitleView title;
    private MyCamera mMyCamera;

    @BindView(R.id.sb_sensitivity) SeekBar sb_sensitivity;
    private HiChipDefines.HI_P2P_S_MD_PARAM md_param;

    @BindView(R.id.tv_sensitivity_rate) TextView tv_sensitivity_rate;
    @BindView(R.id.tv_application) TextView tv_application;

    @BindView(R.id.ll_motion_area) LinearLayout ll_motion_area;
    private boolean mIsVisiableVMDArea=false;
    @BindView(R.id.cropimageview) CropImageView cropimageview;
    @BindView(R.id.rg_area) RadioGroup rg_area;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_adjust_vmd;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        getIntentData();
        initViewAndData();
        setListeners();
    }

    private void setListeners() {
        rg_area.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbtn_left_area:
                        cropimageview.setArea(1);
                        break;
                    case R.id.rbtn_all_area:
                        cropimageview.setArea(2);
                        break;
                    case R.id.rbtn_right_area:
                        cropimageview.setArea(3);
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mMyCamera != null) {
            mMyCamera.registerIOSessionListener(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mMyCamera != null) {
            mMyCamera.unregisterIOSessionListener(this);
        }
    }


    private void initViewAndData() {
        title.setTitle(getString(R.string.title_vmd));
        title.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        title.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which) {
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        finish();
                        break;
                }
            }
        });

        if(mMyCamera.getCommandFunction(HiChipDefines.HI_P2P_SET_MD_PARAM_NEW)&&!mMyCamera.isFishEye()){
            mIsVisiableVMDArea=true;
            cropimageview.getMainSteam(mMyCamera.u32Resolution);
        }else {
            mIsVisiableVMDArea=false;
        }

        ll_motion_area.setVisibility(mIsVisiableVMDArea?View.VISIBLE:View.GONE);
        sb_sensitivity.setMax(100);

        HiChipDefines.HI_P2P_S_MD_PARAM mdparam = new HiChipDefines.HI_P2P_S_MD_PARAM(0, new HiChipDefines.HI_P2P_S_MD_AREA(HiChipDefines.HI_P2P_MOTION_AREA_1, 0, 0, 0, 0, 0, 0));
        mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_MD_PARAM, mdparam.parseContent());

        sb_sensitivity.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress >= 1 && progress <= 100) {
                    tv_sensitivity_rate.setText(progress + "");
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        tv_application.setOnClickListener(this);
    }

    private void getIntentData() {
        String str_uid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
        if (!TextUtils.isEmpty(str_uid)) {
            for (MyCamera camera : HiDataValue.CameraList) {
                if (str_uid.equals(camera.getUid())) {
                    this.mMyCamera = camera;
                    break;
                }
            }
        } else {
            finish();
            return;
        }
    }

    @Override
    public void receiveSessionState(HiCamera hiCamera, int i) {

    }

    @Override
    public void receiveIOCtrlData(HiCamera arg0, int arg1, byte[] arg2, int arg3) {
        if (arg0 != mMyCamera) return;
        Bundle bundle = new Bundle();
        bundle.putByteArray(HiDataValue.EXTRAS_KEY_DATA, arg2);
        Message msg = mHandler.obtainMessage();
        msg.what = HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL;
        msg.obj = arg0;
        msg.arg1 = arg1;
        msg.arg2 = arg3;
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL:
                    if (msg.arg2 == 0) {//成功
                        handReceiveIOCTRLSuccess(msg);
                    } else {
                        handReceiveIOCTRLFail(msg);
                    }
                    break;
            }
        }
    };

    private void handReceiveIOCTRLSuccess(Message msg) {
        Bundle bundle = msg.getData();
        byte[] data = bundle.getByteArray(HiDataValue.EXTRAS_KEY_DATA);
        switch (msg.arg1) {
            case HiChipDefines.HI_P2P_GET_MD_PARAM:
                md_param = new HiChipDefines.HI_P2P_S_MD_PARAM(data);
                if (md_param.struArea.u32Area == HiChipDefines.HI_P2P_MOTION_AREA_1) {
                    int sensitivity = md_param.struArea.u32Sensi;
                    if(sensitivity>=1&&sensitivity<=100){
                        sb_sensitivity.setProgress(sensitivity);
                        tv_sensitivity_rate.setText(sensitivity+"");
                    }
                    handSensiArea();
                }
                break;
            case HiChipDefines.HI_P2P_SET_MD_PARAM:
                dismissjuHuaDialog();
                break;
            case HiChipDefines.HI_P2P_SET_MD_PARAM_NEW:
                dismissjuHuaDialog();
                break;
        }
    }

    private void handReceiveIOCTRLFail(Message msg) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_application:
                if(mIsVisiableVMDArea){
                    sendMotionDetectionExt();
                }else {
                    sendMotionDetection();
                }
                break;
        }
    }

    private void sendMotionDetectionExt() {
        if (md_param == null) {
            return;
        }
        int md = Integer.parseInt(tv_sensitivity_rate.getText().toString());
        md_param.struArea.u32Sensi = md;
        md_param.struArea.u32Width= cropimageview.getmDrawableFloatWidth();
        md_param.struArea.u32Height= cropimageview.getmDrawableFloatHeight();
        md_param.struArea.u32Y= cropimageview.getmDrawableFloatY();
        md_param.struArea.u32X= cropimageview.getmDrawableFloatX();
        showjuHuaDialog();
        mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_MD_PARAM_NEW, md_param.parseContent());

    }

    private void sendMotionDetection() {
        if (md_param == null) {
            return;
        }
        int md = Integer.parseInt(tv_sensitivity_rate.getText().toString());
        md_param.struArea.u32Sensi=md;
        showjuHuaDialog();
        mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_MD_PARAM, md_param.parseContent());
    }

    private void handSensiArea() {
        if(md_param!=null){
            int X= md_param.struArea.u32X;
            int Y=md_param.struArea.u32Y;
            int width=md_param.struArea.u32Width;
            int height=md_param.struArea.u32Height;
            if(mMyCamera.snapshot!=null){
                cropimageview.setDrawable(mMyCamera.snapshot,width,height,X,Y);
            }else {
                Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.videoclip);
                cropimageview.setDrawable(bitmap,width,height,X,Y);
            }

        }
    }
}
